package OperationSystem.day1201.线程安全_syncchronized.演示内存的可见性;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class TestDemo {//演示内存的可见性问题
    private static boolean running=true;

    private static class ChildThread extends Thread{
        @Override
        public void run() {
            int n=0;
            while (running){
                n++;
            }
            System.out.println(n);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t=new ChildThread();
        t.start();

        Scanner scanner=new Scanner(System.in);
        System.out.println("随便输入使子线程退出");
        scanner.nextLine();

        System.out.println(running);
        running=false;
        System.out.println(running);
        while (true){
            System.out.println(t.getState());
            TimeUnit.SECONDS.sleep(1);
        }
    }
}
